<?php

namespace App\Admin\Controllers;

use App\Branches;
use App\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use App\Company;
use Illuminate\Support\Facades\Hash;


class UserController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('用户管理')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('用户管理')
            ->description('详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('用户管理')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('用户管理')
            ->description('新增')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);
        $grid->filter(function ($filter)
        { //1.时间段筛选   设置created_at字段的范围查询
            $filter->between('created_at', '筛选时间')->datetime();
            //2.字段模糊查询 like = '% %'
            $filter->like('name', '姓名');

            $filter->where(function ($query) {
                $query->whereHas('branches', function ($query) {
                    $query->where('branches_name', 'like', "%{$this->input}%");
                });

            }, '网点名称');
            //4.去掉默认的ID搜索
            $filter->disableIdFilter();
            });
        $grid->id('ID');
        $grid->name('用户姓名');
        $grid->email('Email');
      /*  $grid->sex('性别')->using(['1' => '女', '0' => '男']);*/
        $grid->Company()->company_name('所属公司');
        $grid->type('用户类型')->using(['0'=>'个人','1'=>'公司','2'=>'代理','3'=>'生产厂商','4'=>'合作厂商','5'=>'投资方']);
        $grid->money('余额');
        $grid->nickname('微信名');
        $grid->mobile('电话号码');
        $grid->branches()->branches_name('网点名称');
        $grid->deposit('押金');
        $grid->created_at('添加时间');
        $grid->updated_at('编辑时间');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->id('ID');
        $show->name('用户姓名');
        $show->email('Email');
      /*  $show->sex('性别')->using(['1' => '女', '0' => '男']);*/
        $show->Company()->company_name('所属公司');
        $show->type('用户类型')->using(['0'=>'个人','1'=>'公司','2'=>'代理','3'=>'生产厂商','4'=>'合作厂商','5'=>'投资方']);
        $show->created_at('添加时间');
        $show->updated_at('编辑时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);
        $form->text('name','用户名称/公司名称')->rules("required");
        $form->text('phone','电话号码');
        $form->password('password','密码');
        $form->saving(function (Form $form){
            if ($form->password && $form->model()->password != $form->password){
                $form->password=Hash::make($form->password);
            }
        });
        $form->select('type','用户类型')->options(['0'=>'个人','1'=>'公司','2'=>'代理','3'=>'生产厂商','4'=>'合作厂商','5'=>'投资方']);
        $form->select('company_id','所属公司')->options(Company::getSelectOptions());
        $form->select('branches_id','所属网点')->options(Branches::getSelectOptions());
        $form->text('remember_token','remember_token');
        return $form;
    }
}
